using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using SharpCore.Data;
using SharpCore.Extensions;
using SharpCore.Utilities;

namespace MMS.DAO
{
	public class OrderFormDAO
	{
		#region Fields

		private string connectionStringName;

		#endregion

		#region Constructors

		public OrderFormDAO(string connectionStringName)
		{
			ValidationUtility.ValidateArgument("connectionStringName", connectionStringName);

			this.connectionStringName = connectionStringName;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Saves a record to the OrderForm table.
		/// </summary>
		public void Insert(OrderFormDTO orderForm)
		{
			ValidationUtility.ValidateArgument("orderForm", orderForm);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", orderForm.Id),
				new SqlParameter("@IdCustomer", orderForm.IdCustomer),
				new SqlParameter("@Vat", orderForm.Vat),
				new SqlParameter("@Discount", orderForm.Discount),
				new SqlParameter("@Building", orderForm.Building)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormInsert", parameters);
		}

        /// <summary>
        /// Saves a record to the OrderForm table.
        /// </summary>
        public string Insert2(OrderFormDTO orderForm)
        {
            ValidationUtility.ValidateArgument("orderForm", orderForm);

            SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", orderForm.Id),
				new SqlParameter("@IdCustomer", orderForm.IdCustomer),
				new SqlParameter("@Vat", orderForm.Vat),
				new SqlParameter("@Discount", orderForm.Discount),
				new SqlParameter("@Building", orderForm.Building),
                new SqlParameter("@OrderId", SqlDbType.NChar)
			};
            parameters[5].Direction = ParameterDirection.Output;
            parameters[5].Size = 10;

            SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormInsert2", parameters);

            return parameters[5].Value.ToString();            
        }

		/// <summary>
		/// Updates a record in the OrderForm table.
		/// </summary>
		public void Update(OrderFormDTO orderForm)
		{
			ValidationUtility.ValidateArgument("orderForm", orderForm);

			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", orderForm.Id),
				new SqlParameter("@IdCustomer", orderForm.IdCustomer),
				new SqlParameter("@Vat", orderForm.Vat),
				new SqlParameter("@Discount", orderForm.Discount),
				new SqlParameter("@Building", orderForm.Building)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormUpdate", parameters);
		}

		/// <summary>
		/// Deletes a record from the sp_OrderForm table by its primary key.
		/// </summary>
		public void Delete(string id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormDelete", parameters);
		}

		/// <summary>
		/// Deletes a record from the OrderForm table by a foreign key.
		/// </summary>
		public void DeleteAllByIdCustomer(string idCustomer)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCustomer", idCustomer)
			};

			SqlClientUtility.ExecuteNonQuery(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormDeleteAllByIdCustomer", parameters);
		}

		/// <summary>
		/// Selects a single record from the OrderForm table.
		/// </summary>
		public OrderFormDTO Select(string id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormSelect", parameters))
			{
				if (dataReader.Read())
				{
					return MapDataReader(dataReader);
				}
				else
				{
					return null;
				}
			}
		}

		/// <summary>
		/// Selects a single record from the OrderForm table.
		/// </summary>
		public string SelectJson(string id)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@Id", id)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormSelect", parameters);
		}

		/// <summary>
		/// Selects all records from the OrderForm table.
		/// </summary>
		public List<OrderFormDTO> SelectAll()
		{
			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormSelectAll"))
			{
				List<OrderFormDTO> orderFormDTOList = new List<OrderFormDTO>();
				while (dataReader.Read())
				{
					OrderFormDTO orderFormDTO = MapDataReader(dataReader);
					orderFormDTOList.Add(orderFormDTO);
				}

				return orderFormDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the OrderForm table.
		/// </summary>
		public string SelectAllJson()
		{
			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormSelectAll");
		}

		/// <summary>
		/// Selects all records from the OrderForm table by a foreign key.
		/// </summary>
		public List<OrderFormDTO> SelectAllByIdCustomer(string idCustomer)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCustomer", idCustomer)
			};

			using (SqlDataReader dataReader = SqlClientUtility.ExecuteReader(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormSelectAllByIdCustomer", parameters))
			{
				List<OrderFormDTO> orderFormDTOList = new List<OrderFormDTO>();
				while (dataReader.Read())
				{
					OrderFormDTO orderFormDTO = MapDataReader(dataReader);
					orderFormDTOList.Add(orderFormDTO);
				}

				return orderFormDTOList;
			}
		}

		/// <summary>
		/// Selects all records from the OrderForm table by a foreign key.
		/// </summary>
		public string SelectAllByIdCustomerJson(string idCustomer)
		{
			SqlParameter[] parameters = new SqlParameter[]
			{
				new SqlParameter("@IdCustomer", idCustomer)
			};

			return SqlClientUtility.ExecuteJson(connectionStringName, CommandType.StoredProcedure, "sp_OrderFormSelectAllByIdCustomer", parameters);
		}

		/// <summary>
		/// Creates a new instance of the OrderFormDTO class and populates it with data from the specified SqlDataReader.
		/// </summary>
		private OrderFormDTO MapDataReader(SqlDataReader dataReader)
		{
			OrderFormDTO orderFormDTO = new OrderFormDTO();
			orderFormDTO.Id = dataReader.GetString("Id", String.Empty);
			orderFormDTO.IdCustomer = dataReader.GetString("IdCustomer", String.Empty);
			orderFormDTO.Vat = dataReader.GetInt32("Vat", 0);
			orderFormDTO.Discount = dataReader.GetInt32("Discount", 0);
			orderFormDTO.Building = dataReader.GetString("Building", null);

			return orderFormDTO;
		}

		#endregion
	}
}
